#version 300 es
precision mediump float;
uniform sampler2D sTexture;
uniform bool bSelect  ;

uniform float uf_LeftTop ;
uniform float uf_CenterTop  ;
uniform float uf_RightTop  ;

uniform float uf_LeftCenter ;
uniform float uf_CenterCenter ;
uniform float uf_RightCenter  ;


uniform float uf_LeftBottom;
uniform float uf_CenterBottom ;
uniform float uf_RightBottom ;

uniform float uf_ScaleFactor ;

uniform float uf_MagTol ;
uniform float uf_Quantize ;

in vec2 vTextureCoord;

out vec4 fFragColor;
void main()
{
    if(bSelect){
        ivec2 ires=textureSize(sTexture,0);
        float uResS=float(ires.s);
        float uResT=float(ires.t);
        vec3 rgb=texture(sTexture,vTextureCoord).rgb;

        vec2 stp0=vec2(1.0/uResS,0.0);
        vec2 st0p=vec2(0.0,1.0/uResT);
        vec2 stpp=vec2(1.0/uResS,1.0/uResT);
        vec2 stpm=vec2(1.0/uResS,-1.0/uResT);

        const vec3 W=vec3(0.2125,0.7154,0.0721);

        float scaleFactor = uf_ScaleFactor ;
       	float kernelValue0 = uf_LeftTop;
       	float kernelValue1 = uf_CenterTop;
       	float kernelValue2 = uf_RightTop;

       	float kernelValue3 = uf_LeftCenter;
       	float kernelValue4 = uf_CenterCenter;
      	float kernelValue5 = uf_RightCenter;

      	float kernelValue6 = uf_LeftBottom;
       	float kernelValue7 = uf_CenterBottom;
     	float kernelValue8 = uf_RightBottom;
       	vec4 sum;

       	vec4 cTemp0,cTemp1,cTemp2,cTemp3,cTemp4,cTemp5,cTemp6,cTemp7,cTemp8;
       	cTemp0=texture(sTexture, vTextureCoord.st - stpp); //  左上
       	cTemp1=texture(sTexture, vTextureCoord.st - st0p); //  中上
       	cTemp2=texture(sTexture, vTextureCoord.st + stpm); //  右上
       	cTemp3=texture(sTexture, vTextureCoord.st - stp0);  // 中左
       	cTemp4=texture(sTexture, vTextureCoord.st );        // 中中
       	cTemp5=texture(sTexture, vTextureCoord.st + stp0);  // 中右
       	cTemp6=texture(sTexture, vTextureCoord.st - stpm);  // 左下
       	cTemp7=texture(sTexture, vTextureCoord.st + st0p); // 中下
       	cTemp8=texture(sTexture, vTextureCoord.st + stpp); //右下

        float fTemp0 ,fTemp1,fTemp2,fTemp3,fTemp4,fTemp5,fTemp6,fTemp7,fTemp8;
        fTemp0 = dot(cTemp0.rgb , W) ;
        fTemp1 = dot(cTemp1.rgb , W) ;
        fTemp2 = dot(cTemp2.rgb , W) ;
        fTemp3 = dot(cTemp3.rgb , W) ;
        fTemp4 = dot(cTemp4.rgb , W) ;
        fTemp5 = dot(cTemp5.rgb , W) ;
        fTemp6 = dot(cTemp6.rgb , W) ;
        fTemp7 = dot(cTemp7.rgb , W) ;
        fTemp8 = dot(cTemp8.rgb , W) ;

        float h = -1.0 * fTemp6 - 2.0 * fTemp7 - 1.0 * fTemp8 + 1.0 * fTemp0 + 2.0 * fTemp1 + 1.0 * fTemp2;
        float v = -1.0 * fTemp0 - 2.0 * fTemp3 - 1.0 * fTemp6 + 1.0 * fTemp2 + 2.0 * fTemp5 + 1.0 * fTemp8;
        float mag=length(vec2(h,v));
        bool test = true ;
        if(test){
            if(mag>uf_MagTol){
                fFragColor=vec4(0.0,0.0,0.0,1.0);
            }else{
                rgb.rgb *= uf_Quantize;
                rgb.rgb += vec3(0.5,0.5,0.5);
                ivec3 intrgb = ivec3(rgb.rgb);
                rgb.rgb = vec3(intrgb) / uf_Quantize;
                fFragColor=vec4(rgb,1.0);

            }

        }else{

                sum =kernelValue0*cTemp0+kernelValue1*cTemp1+kernelValue2*cTemp2+
               		 kernelValue3*cTemp3+kernelValue4*cTemp4+kernelValue5*cTemp5+
               	     kernelValue6*cTemp6+kernelValue7*cTemp7+kernelValue8*cTemp8;

                fFragColor = sum * scaleFactor;

        }


    }else{
    fFragColor = texture(sTexture, vTextureCoord);
    }



}
